package UML_ER.diagram.edit.commands;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.AbstractEditCommandRequest;

import UML_ER.Attribute;
import UML_ER.AttributeType;


public class ApplyUnapplyAttributeTypeCommand extends EditElementCommand {

	public ApplyUnapplyAttributeTypeCommand(ApplyUnapplyAttributeTypeRequest request) {
		super(request.getAttributeType() == AttributeType.STANDARD ? "UnApplyAttributeType" : "ApplyAttributeType", request
			.getAttribute(), request);
	}

	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		ApplyUnapplyAttributeTypeRequest request = (ApplyUnapplyAttributeTypeRequest) getRequest();
		if (request.getAttributeType() == AttributeType.PRIMARY_KEY) {
			request.getAttribute().setAttributeType(AttributeType.PRIMARY_KEY);}
		if (request.getAttributeType() == AttributeType.DERIVED) {
			request.getAttribute().setAttributeType(AttributeType.DERIVED);
		}
		if (request.getAttributeType() == AttributeType.MULTI_VALUED) {
			request.getAttribute().setAttributeType(AttributeType.MULTI_VALUED);
		}
		if (request.getAttributeType() == AttributeType.STANDARD) {
			request.getAttribute().setAttributeType(AttributeType.STANDARD);
		}
		return CommandResult.newOKCommandResult(request.getAttribute());
	}

	public static class ApplyUnapplyAttributeTypeRequest extends AbstractEditCommandRequest {

		private final Attribute myAttribute;
		
		private final AttributeType myAttributeType;

		public ApplyUnapplyAttributeTypeRequest(Attribute attribute,  AttributeType attributeType) {
			super(TransactionUtil.getEditingDomain(attribute));
			myAttribute = attribute;
			myAttributeType = attributeType;
		}


		public Attribute getAttribute() {
			return myAttribute;
		}

		
		public AttributeType getAttributeType() {
			return myAttributeType;
		}

		public Object getEditHelperContext() {
			return null;
		}

	}
}
